package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "帖子接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;

    @ApiOperation("获取版块下帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@ApiParam("版块id") @RequestParam(value = "boardId",required = false) Long boardId){
        //声明返回的集合
        List<Article> articles;
        //根据传入的id获取不同的集合
        if(boardId==null){
            //首页
            articles = articleService.selectAll();
        }else{
            //查询对应版块帖子集合
            articles=articleService.selectByBoardId(boardId);
        }
        //判断是否为空
        if(articles==null){
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }

    @ApiOperation("发布帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("版块id") @RequestParam("boardId") @NonNull Long boardId,
                            @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("帖子内容") @RequestParam("content") @NonNull String content){
        //获取用户信息
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //校验用户状态
        if(user.getState()==1){
            //用户已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //构造帖子对象
        Article article=new Article();
        article.setUserId(user.getId());
        article.setBoardId(boardId);
        article.setTitle(title);
        article.setContent(content);
        //调用service
        articleService.create(article);
        //返回结果
        return AppResult.success("发布成功！！！");
    }


    @ApiOperation("获取帖子详情")
    @GetMapping("/getById")
    public AppResult<Article> getDetails(HttpServletRequest request,
                                         @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        //调用service
        Article article = articleService.selectById(id);
        //校验
        if(article==null){
            return AppResult.failed("帖子不存在！");
        }
        
        //更新帖子访问次数
        articleService.updateVisitCountById(id);
        //返回之前更新返回对象的访问次数
        article.setVisitCount(article.getVisitCount()+1);
        
        //获取当前登录的用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //判断是不是自己发布的文章
        if(user.getId()==article.getUserId()){
            article.setOwn(true);
        }
        //返回结果
        return AppResult.success(article);
    }

    @ApiOperation("编辑帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id,
                            @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("帖子内容") @RequestParam("content") @NonNull String content){
        //从session中获取当前登录用户
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //校验用户
        if(user.getState()==1){
            //禁言状态
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子信息
        Article article = articleService.selectById(id);
        //校验帖子是否存在
        if(article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断当前用户是否为作者
        if(article.getUserId()!=user.getId()){
            return AppResult.failed("非作者无权修改😊");
        }
        //校验帖子状态
        if(article.getState()==1){
            return AppResult.failed("帖子状态异常");
        }
        //调用service
        articleService.modify(id,title,content);
        //返回结果
        return AppResult.success("修改成功");
    }

    @ApiOperation("获取用户帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult getAllByUserId(HttpServletRequest request,
                                    @ApiParam("用户id") @RequestParam(value = "userId",required = false) Long userId){
        //若userId为空
        if(userId==null){
            //从session获取
            HttpSession session = request.getSession(false);
            User user  = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
            userId=user.getId();
        }
        //调用service
        List<Article> articles = articleService.selectByUserId(userId);
        //判断是否为空
        if(articles==null){
            articles=new ArrayList<>();
        }
        //返回结果
        return AppResult.success(articles);
    }

    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult delete(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        //获取session
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);

        //校验用户
        if(user.getState()==1){
            //禁言状态
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子对象
        Article article=articleService.selectById(id);
        //校验帖子是否存在
        if(article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断当前文章对象是否为登录用户
        if(user.getId()!= article.getUserId()){
            return AppResult.failed("非作者无权删除");
        }
        //校验帖子状态
        if(article.getState()==1){
            return AppResult.failed("帖子状态异常");
        }
        //调用service
        articleService.delete(article);
        //返回结果
        return AppResult.success("删除成功");
    }

    @ApiOperation("点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,
                              @ApiParam(value = "帖⼦Id") @RequestParam(value = "id") @NonNull Long id) {
        //获取session
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //校验用户
        if(user.getState()==1){
            //禁言状态
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //更新点赞数
        articleService.thumbsUpById(id);
        //返回结果
        return AppResult.success();
    }
}
